package com.code.translate.httpbuilder;

import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * @author ggh
 * @date 2022/8/9
 */
public class DefaultBuilder extends ContentTypeBuilder {

    @Override
    public void bodybuilder(OutputStream os, Map<String, Object> params) throws IOException {
        StringBuilder paramBuilder = new StringBuilder();
        if (!CollectionUtils.isEmpty(params)) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                paramBuilder.append(entry.getKey() + "=" + entry.getValue().toString() + "&");
            }
            paramBuilder.deleteCharAt(paramBuilder.length() - 1);
        }
        os.write(paramBuilder.toString().getBytes());
    }

    @Override
    public boolean isPresent(String content) {
        return true;
    }
}
